package adventure.commands;

import adventure.*;
import player.*;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class GoMove extends AbstractCommand {

    public GoMove(Adventure a) {
        super(a, new String[]{"GO", "MOVE"});
    }

    @Override
    public String validateAndExecute(Player p, String[] args) {
        if (args.length == 2) {
            if (Util.isDirection(args[1])) {
                return move(p, args[1].charAt(0));
            } else {
                return "Move where?";
            }
        }
        return "Command not recognised.";

    }

    protected String move(Player player, char dir) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        if (player.inCombat()) {
            return "You cannot do that in combat!";
        } else {
            if (!rooms.isBlocked(x, y, z, dir)) {
                if (rooms.isClosed(x, y, z, dir)) {
                    return "That direction is closed!";
                } else {
                    String direction = Util.fillDir(dir);
                    if (direction.equals("upper")) {
                        direction = "for above";
                    } else if (direction.equals("lower")) {
                        direction = "for below";
                    } else {
                        direction = "to the " + direction.substring(0, direction.length() - 3);
                    }

                    if (out != null) {
                        out.broadcastLocalToPlayer(player,
                                ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                                + " leaves " + direction + ".");
                    }


                    Player[] followers = player.getLocalFollowers();

                    for (int c = 0; c < followers.length; c++) {
                        rooms.movePlayer(followers[c], dir);
                        // Add speedwalk
                        if (followers[c].recordingSpeedwalk()) {
                            followers[c].addToSpeedwalk(dir);
                        }
                    }

                    if (out != null) {
                        out.broadcastLocalToPlayers(followers,
                                ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                                + " arrives from " + Util.oppositeDirection(dir) + ".");
                        

                        // Broadcast move for all of the party
                        for (int c = 1; c < followers.length; c++) {
                            out.broadcastToPlayer(followers[c], "\n"+move(followers[c], dir));
                        }
                    }

                    // Get the output for the player
                    String output = adv.printRoom(player);
                    if (rooms.updatePlayerSpawn(player)) {
                        output += "\n\nYour respawn location has been "
                                + "updated to this location.";
                    }
                    
                    // Trip triggers upon entering rooms
                    String triggerOut = adv.tripTriggers(player);
                    if (triggerOut.length() > 0) {
                        output += "\n" + triggerOut;
                    }

                    // Aggro mobs attack
                    String aggroOut = rooms.aggroMobsAttack(player);
                    if (aggroOut.length() > 0) {
                        output += "\n\n" + aggroOut;
                    }
                    return output;
                }
            } else {
                return "You cannot move any further in that direction!";
            }
        }
    }
}
